package com.svms.service.validators;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.svms.service.constants.Constants;
import com.svms.service.constants.SvmsStatusCodes;
import com.svms.service.factory.SvmsServiceFactory;
import com.svms.service.util.SvmsDateUtils;
import com.svms.service.vo.ResultCodeVO;
import com.svms.service.vo.VehicleVO;

public final class VehicleValidator {

	private VehicleValidator() {

	}

	private static Logger logger = Logger.getLogger(VehicleValidator.class);

	public static boolean isDuplicateVehicle(VehicleVO vehicle) {
		boolean isDuplicate = true;
		VehicleVO vehicleFromDb = SvmsServiceFactory.getInstance().getVehicle()
				.getVehicle(vehicle);
		List<String> errorDetails = vehicleFromDb.getResultCode()
				.getErrorDetails();
		if (errorDetails.size() > 0) {
			if (errorDetails.contains(SvmsStatusCodes.NO_VEHILCE_FOUND)) {
				isDuplicate = false;
			}
		}
		return isDuplicate;
	}

	public static boolean findRegularExp(VehicleVO vehicle) {
		boolean isvalid = false;
		if (StringUtils.isAlphanumeric(vehicle.getModel().trim())
				&& StringUtils
						.isAlphanumeric(vehicle.getVehicleNumber().trim())
				&& StringUtils.isNumeric(vehicle.getDistanceTraveled().trim())
				&& StringUtils.isNumeric(vehicle.getTransport()
						.getTransportId().trim())) {
			isvalid = true;
		}
		return isvalid;
	}

	/**
	 * To validate if VehicleVO entered is valid
	 * 
	 * @param vehicle
	 * @return
	 */
	public static VehicleVO validateVehicle(VehicleVO vehicle) {

		ResultCodeVO resultCodeVO = new ResultCodeVO();

		if (vehicle != null) {

			String vehicleNumber = StringUtils.trim(vehicle.getVehicleNumber());
			String vehicleModel = StringUtils.trim(vehicle.getModel());
			String transportId = StringUtils
					.trim(vehicle.getTransport() != null ? vehicle
							.getTransport().getTransportId() : null);
			String distanceTraveled = StringUtils.trim(vehicle
					.getDistanceTraveled());
			Date form74 = null;
			Date fc = null;
			Date nc = null;
			Date insurance = null;
			Date tax = null;

			Map<String, Date> vehicleDocs = vehicle.getVehicleDocuments();

			if (vehicleDocs != null && vehicleDocs.size() > 0) {
				form74 = vehicleDocs.get(Constants.FORM47_TD);
				fc = vehicleDocs.get(Constants.FC_TD);
				nc = vehicleDocs.get(Constants.NC_TD);
				insurance = vehicleDocs.get(Constants.INSURANCE_TD);
				tax = vehicleDocs.get(Constants.TAX_TD);
			}

			if (StringUtils.isEmpty(vehicleNumber)) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.VEHICLE_NUMBER_IS_MANDATORY);
			} else if (vehicleNumber.contains("%")
					|| vehicleNumber.contains("?")) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.INVALID_VEHICLE_NUMBER_ENTERED);
			}
			if (StringUtils.isEmpty(vehicleModel)) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.VEHICLE_MODEL_IS_MANDATORY);
			} else if (vehicleModel.contains("%") || vehicleModel.contains("?")) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.INVALID_VEHICLE_MODEL_ENTERED);
			}
			if (StringUtils.isEmpty(transportId)) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.TRANSPORT_IS_MANDATORY_FOR_VEHICLE);
			} else if (transportId.contains("%") || transportId.contains("?")) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.INVALID_TRANSPORT_ENTERED);
			}
			/*
			 * if (distanceTraveled == null) { resultCodeVO
			 * .addErrorDetail(SvmsStatusCodes.DISTANCE_TRAVELED_IS_MANDATORY);
			 * } else if (transportId.contains("%") ||
			 * transportId.contains("?")) { resultCodeVO
			 * .addErrorDetail(SvmsStatusCodes
			 * .INVALID_VALUE_ENTERED_FOR_DISTANCE_TRAVELED_FIELD); }
			 */

			if (form74 == null) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.FORM_47_DATE_IS_MANDATORY);
			} else if (SvmsDateUtils.isToday(form74)
					|| SvmsDateUtils.isBeforeDay(form74, new Date())) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.FORM_47_DATE_ENTERED_IS_INVALID_);
			}
			if (fc == null) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.FC_DATE_IS_MANDATORY);
			} else if (SvmsDateUtils.isToday(fc)
					|| SvmsDateUtils.isBeforeDay(fc, new Date())) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.FC_DATE_ENTERED_IS_INVALID);
			}
			if (nc == null) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.NC_DATE_IS_MANDATORY);
			} else if (SvmsDateUtils.isToday(nc)
					|| SvmsDateUtils.isBeforeDay(nc, new Date())) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.NC_DATE_ENTERED_IS_INVALID);
			}
			if (insurance == null) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.INSURANCE_DATE_IS_MANDATORY);
			} else if (SvmsDateUtils.isToday(insurance)
					|| SvmsDateUtils.isBeforeDay(insurance, new Date())) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.INSURANCE_DATE_ENTERED_IS_INVALID);
			}
			if (tax == null) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.TAX_DATE_IS_MANDATORY);
			} else if (SvmsDateUtils.isToday(tax)
					|| SvmsDateUtils.isBeforeDay(tax, new Date())) {
				resultCodeVO
						.addErrorDetail(SvmsStatusCodes.TAX_DATE_ENTERED_IS_INVALID);
			}
		} else {
			resultCodeVO
					.addErrorDetail(SvmsStatusCodes.NO_VEHICLE_DATA_PRESENT_FOR_EDIT);
		}

		vehicle.setResultCode(resultCodeVO);

		return vehicle;
	}
}
